/*
PROJECT:            Aflasafe Kenya / Impact of surveys
OPERATED BY:        Mark Treurniet
DATE LAST MODIFIED: 2021-09-10
PURPOSE:            Analysis for paper "The Impact of Being Surveyed on the Adoption of Agricultural Technology", Economic Development and Cultural Change, forthcoming
*/

*INITIALIZE
cls
cd "M:\My Documents\02 Maize Kenya\Impact of surveys\Stata"
log using ".\04 logs\analysis_impact_surveys.log", replace
set more off
set matsize 10000

*LOAD DATA
use ".\03 data\impact_surveys", clear

*PREPARE VARIABLES
**Prepare IVs
tab All_survey_status, generate(All_survey_status_)
label variable All_survey_status_1 "Primary"
label variable All_survey_status_2 "Replacement 1"
label variable All_survey_status_3 "Replacement 2"
label variable All_survey_status_4 "Replacement 3"
label variable All_survey_status_5 "Replacement 4"
label variable All_survey_status_6 "Replacement 5"
label variable All_survey_status_7 "Replacement 6"
forvalues i=1/7 {
	replace All_survey_status_`i' = 0 if All_survey_status==""
	}
gen All_survey_status_replacement = All_survey_status_2 | All_survey_status_3 | All_survey_status_4 | All_survey_status_5 | All_survey_status_6 | All_survey_status_7
gen All_survey_status_any = All_survey_status_1 | All_survey_status_replacement
**Prepare subgroup fixed effects
qui tab groupid, gen(groupid_)
foreach var of varlist groupid_* {
	qui gen int_All_present_`var' = All_present*`var'
	}
**Prepare interactions
gen int_baseline_sample_primary = baseline_sample*All_survey_status_1
foreach var of varlist baseline_sample All_survey_status_1-All_survey_status_7 {
	gen int_treat_`var' = Treat_v_market_linkage*`var'
	}
**Labels
label variable baseline_sample             "Surveyed"
label variable int_baseline_sample_primary "Surveyed*Primary"
label variable int_treat_baseline_sample   "Market linkage*Surveyed"
label variable intensity_kg                "Amount (kg)"

*PREPARE VARIABLE LISTS
**Variables used in main analysis
global depvars_adoption      adoption intensity_kg
global instruments           All_survey_status_1 All_survey_status_2 All_survey_status_3 All_survey_status_4 All_survey_status_5 All_survey_status_6 All_survey_status_7
global int_treat_instruments int_treat_All_survey_status_1-int_treat_All_survey_status_7
global controls_IV           groupid_* int_All_present_groupid_*
global balance               age_resp edu_HH relation_head assets land_maize maize_harvest maize_market expenditure_total social_capital knowledge_index insurance trust qual_risk_aversion
**Vector of baseline controls used in Hoffmann et al. (2020)
global controls_GC			 All_present All_sex
foreach var of varlist $balance {
	global controls_BL_I_ws $controls_BL_I_ws `var'_grp
	}
global controls_BL_G		 county_1 county_2 county_3 group_capacity gender
global controls_Rain		 Rain_trigger_vegetative_drought Rain_trigger_flowering_drought Rain_trigger_ripening_excess
global controls_ws_b		 $controls_GC baseline_sample $controls_BL_I_ws $controls_BL_G $controls_Rain

*DO ANALYSIS
**Sample sizes (Table 1)
***Present in group census vs. surveyed at baseline
qui putexcel set ".\06 tables\table_sample_sizes.xlsx", replace
qui putexcel C2  = "Surveyed"
qui putexcel D2  = "Not surveyed"
qui putexcel E2  = "Total"
qui putexcel A3  = "Present in group census meeting"
qui putexcel B3  = "Primary list"
qui putexcel B4  = "Replacement list"
qui putexcel B5  = "Control list"
qui putexcel B6  = "Total"
qui putexcel A7  = "Not present in group census meeting"
qui putexcel B7  = "Primary list"
qui putexcel B8  = "Replacement list"
qui putexcel B9  = "Control list"
qui putexcel B10 = "Total"
qui putexcel A11 = "Total"
qui putexcel B11 = "Primary list"
qui putexcel B12 = "Replacement list"
qui putexcel B13 = "Control list"
qui putexcel B14 = "Total"
local row = 3
local cond1 "&  All_present"
local cond2 "& !All_present"
local cond3 ""
forvalues i=1/3 {
	qui count if impact_survey_sample `cond`i'' & All_survey_status_1                &  baseline_sample
	qui putexcel C`row' = `r(N)'
	qui count if impact_survey_sample `cond`i'' & All_survey_status_1                & !baseline_sample
	qui putexcel D`row' = `r(N)'
	qui count if impact_survey_sample `cond`i'' & All_survey_status_1
	qui putexcel E`row' = `r(N)'
	local row = `row' + 1
	qui count if impact_survey_sample `cond`i'' & substr(All_survey_status,1,1)=="R" &  baseline_sample
	qui putexcel C`row' = `r(N)'
	qui count if impact_survey_sample `cond`i'' & substr(All_survey_status,1,1)=="R" & !baseline_sample
	qui putexcel D`row' = `r(N)'
	qui count if impact_survey_sample `cond`i'' & substr(All_survey_status,1,1)=="R"
	qui putexcel E`row' = `r(N)'
	local row = `row' + 1
	qui count if impact_survey_sample `cond`i'' & All_survey_status==""              &  baseline_sample
	qui putexcel C`row' = `r(N)'
	qui count if impact_survey_sample `cond`i'' & All_survey_status==""              & !baseline_sample
	qui putexcel D`row' = `r(N)'
	qui count if impact_survey_sample `cond`i'' & All_survey_status==""
	qui putexcel E`row' = `r(N)'
	local row = `row' + 1
	qui count if impact_survey_sample `cond`i'' &                                       baseline_sample
	qui putexcel C`row' = `r(N)'
	qui count if impact_survey_sample `cond`i'' &                                      !baseline_sample
	qui putexcel D`row' = `r(N)'
	qui count if impact_survey_sample `cond`i''
	qui putexcel E`row' = `r(N)'
	local row = `row' + 1
	}

**Summarize variables on knowledge on aflatoxin and experience with aflatoxin prevention measures
sum BL_I_k2_afla_heard a_BL_I_k3_afla_tell BL_I_k5_afla_prevent_1 BL_I_k6_afla_measure_1 BL_I_k7_aflasafe_heard a_BL_I_k8_afla_used if impact_survey_sample
tab a_BL_I_k4_afla_effects if impact_survey_sample
tab a_BL_I_k5_afla_prevent if impact_survey_sample

**Calculate proportion surveyed
sum baseline_sample if impact_survey_sample & All_survey_status_1

**Graph proportion surveyed vs. instruments (Figure 1)
***Among those listed
eststo listed: regress baseline_sample $instruments if impact_survey_sample, vce(cluster groupid)
test All_survey_status_1=All_survey_status_2=All_survey_status_3=All_survey_status_4=All_survey_status_5=All_survey_status_6=All_survey_status_7
test All_survey_status_2=All_survey_status_3=All_survey_status_4=All_survey_status_5=All_survey_status_6=All_survey_status_7
***Among those called
eststo called: qui regress baseline_sample $instruments if impact_survey_sample & considered, nocons vce(cluster groupid)
test All_survey_status_1=All_survey_status_2=All_survey_status_3=All_survey_status_4=All_survey_status_5=All_survey_status_6=All_survey_status_7
test All_survey_status_2=All_survey_status_3=All_survey_status_4=All_survey_status_5=All_survey_status_6=All_survey_status_7
***Make graph
qui coefplot (listed, label("of those listed") color(gs4)) (called, label("of those called") color(gs12)), ///
		keep($instruments) ///
		vertical ///
		recast(bar) barwidth(0.4) ///
		ciopts(recast(rcap) color(black)) citop ///
		///title("Graph 1: Proportion surveyed") ///
		yscale(range(0 .)) ylabel(0(.1).8, angle(horizontal)) ///
		coeflabels(All_survey_status_1 = "P" All_survey_status_2 = "R1" All_survey_status_3 = "R2" All_survey_status_4 = "R3" All_survey_status_5 = "R4" All_survey_status_6 = "R5" All_survey_status_7 = "R6") ///
		graphregion(color(white))
graph export ".\05 graphs\Graph 1 Proportion surveyed.png", replace
graph export ".\05 graphs\Graph 1 Proportion surveyed.pdf", replace
eststo clear
***F-test first stage
regress baseline_sample $instruments $controls_IV if impact_survey_sample, vce(robust)
test All_survey_status_1=All_survey_status_2=All_survey_status_3=All_survey_status_4=All_survey_status_5=All_survey_status_6=All_survey_status_7=0

**Balance at baseline
***Main analysis
ivregress 2sls All_sex $controls_IV (baseline_sample = $instruments) if impact_survey_sample, vce(cluster groupid) //main analysis
***DREO estimator
qui gen Y = All_sex //outcome variable
qui gen D = baseline_sample //treatment variable
qui gen Z = considered //Z is the ever-offer indicator variable: it is equal to 1 for applicants that received an offer to get treated, and to 0 for all other applicants
qui gen W = 10*groupid + All_present //W is the identifier of the waitlist the applicant participated in
qui bysort W: egen S = total(D) //is the number of seats available in the waitlist the applicant participated in. S is equal to the number of applicants who received an offer and got treated in each waitlist
randomizedwaitlist Y D Z W S if impact_survey_sample
scalar p_balance_DREO = 2 * ttail(e(nb_obs), abs(e(DREO_LATE)/e(se_DREO_LATE)))
di scalar(p_balance_DREO)
qui drop Y D Z W S
***Primary list vs. control list
ivregress 2sls All_sex $controls_IV (baseline_sample = All_survey_status_1) if impact_survey_sample & !All_survey_status_replacement, vce(cluster groupid)

*Calculate proportion of farmers for which actual purchases during sales were recorded by their baseline survey enumerator
gen temp_same_person = a_BL_I_a3name==a_Sale_a1foname if !missing(a_BL_I_a3name) & !missing(a_Sale_a1foname)
sum temp_same_person if impact_survey_sample
drop temp_same_person

**Impact of being surveyed
***Main analysis (Table 2)
****Count number of subgroups
local N_group_eff    = 0
local N_subgroup     = 0
local N_subgroup_eff = 0
local N_indiv        = 0
local N_indiv_eff    = 0
tempvar survey_status
gen `survey_status'  = 0
forvalues i=1/7 {
	qui replace `survey_status' = `i'*All_survey_status_`i' if All_survey_status_`i'
	}
tab `survey_status' if impact_survey_sample
foreach subgroup of varlist groupid_* {
	local group_eff = 0
	qui sum `survey_status' if impact_survey_sample &  All_present & `subgroup'
	if `r(N)'>0 {
		local group_eff      = `group_eff' + (`r(max)'>`r(min)')
		local N_subgroup     = `N_subgroup' + 1
		local N_subgroup_eff = `N_subgroup_eff' + (`r(max)'>`r(min)')
		local N_indiv        = `N_indiv' + `r(N)'
		local N_indiv_eff    = `N_indiv_eff' + `r(N)'*(`r(max)'>`r(min)')
		}
	qui sum `survey_status' if impact_survey_sample & !All_present & `subgroup'
	if `r(N)'>0 {
		local group_eff      = `group_eff' + (`r(max)'>`r(min)')
		local N_subgroup     = `N_subgroup' + 1
		local N_subgroup_eff = `N_subgroup_eff' + (`r(max)'>`r(min)')
		local N_indiv        = `N_indiv' + `r(N)'
		local N_indiv_eff    = `N_indiv_eff' + `r(N)'*(`r(max)'>`r(min)')
		}
	if `group_eff'>0 {
		local N_group_eff    = `N_group_eff' + 1
		}
	}
****Regressions
foreach depvar of varlist $depvars_adoption {
	eststo: qui ivregress 2sls `depvar' $controls_IV (baseline_sample = $instruments) if impact_survey_sample, vce(cluster groupid)
		estadd local N_group_eff `N_group_eff'
		estadd local N_subgroup `N_subgroup'
		estadd local N_subgroup_eff `N_subgroup_eff'
		estadd local N_indiv_eff `N_indiv_eff'
		estadd local Controls_I "No"
		estadd local FE "Yes"
		qui sum `depvar' if impact_survey_sample & All_survey_status_1
		estadd scalar MeanPrimary               = r(mean)
		estadd scalar RelativeImpactPrimary     = _b[baseline_sample]/r(mean)
		qui sum `depvar' if impact_survey_sample & (All_survey_status_2|All_survey_status_3|All_survey_status_4|All_survey_status_5|All_survey_status_6|All_survey_status_7)
		estadd scalar MeanReplacement           = r(mean)
		estadd scalar RelativeImpactReplacement = _b[baseline_sample]/r(mean)
		qui sum `depvar' if impact_survey_sample & !(All_survey_status_1|All_survey_status_2|All_survey_status_3|All_survey_status_4|All_survey_status_5|All_survey_status_6|All_survey_status_7)
		estadd scalar MeanControl               = r(mean)
		estadd scalar RelativeImpactControl     = _b[baseline_sample]/r(mean)
	}
foreach depvar of varlist $depvars_adoption {
	eststo: qui ivregress 2sls `depvar' $controls_IV All_sex (baseline_sample = $instruments) if impact_survey_sample, vce(cluster groupid)
		estadd local N_group_eff `N_group_eff'
		estadd local N_subgroup `N_subgroup'
		estadd local N_subgroup_eff `N_subgroup_eff'
		estadd local N_indiv_eff `N_indiv_eff'
		estadd local Controls_I "Yes"
		estadd local FE "Yes"
		qui sum `depvar' if impact_survey_sample & All_survey_status_1
		estadd scalar MeanPrimary               = r(mean)
		estadd scalar RelativeImpactPrimary     = _b[baseline_sample]/r(mean)
		qui sum `depvar' if impact_survey_sample & (All_survey_status_2|All_survey_status_3|All_survey_status_4|All_survey_status_5|All_survey_status_6|All_survey_status_7)
		estadd scalar MeanReplacement           = r(mean)
		estadd scalar RelativeImpactReplacement = _b[baseline_sample]/r(mean)
		qui sum `depvar' if impact_survey_sample & !(All_survey_status_1|All_survey_status_2|All_survey_status_3|All_survey_status_4|All_survey_status_5|All_survey_status_6|All_survey_status_7)
		estadd scalar MeanControl               = r(mean)
		estadd scalar RelativeImpactControl     = _b[baseline_sample]/r(mean)
	}
esttab using ".\06 tables\table_impact.csv", keep(baseline_sample) b(3) star(* 0.10 ** 0.05 *** 0.01) se sfmt(%8.0g %05.3f %05.3f %05.3f %05.3f %05.3f %05.3f %05.3f %05.3f %05.3f %8.0g) scalars( "N_clust Groups" "Controls_I Controlled for gender" "FE Subgroup fixed effects" "MeanPrimary Mean of primary list" "RelativeImpactPrimary Relative impact primary list" "MeanReplacement Mean of replacement list" "RelativeImpactReplacement Relative impact replacement list" "MeanControl Mean of control list" "RelativeImpactControl Relative impact control list" "N_group_eff Groups effective" "N_subgroup Subgroups" "N_subgroup_eff Subgroups effective" "N_indiv_eff Observations effective") title("Impact of being surveyed") label replace
eststo clear
***Robustness analysis (Table 3)
****doubly-reweighted ever-offer (DREO) estimator as proposed by DeChaisemartin and Behaghel (2020) (Table 3, Columns 1-2)
local column_no = 2
putexcel set ".\06 tables\table_robustness_a.xlsx", replace
foreach depvar of varlist $depvars_adoption {
	*****Run regressions
	qui gen Y = `depvar' //outcome variable
	qui gen D = baseline_sample //treatment variable
	qui gen Z = considered //Z is the ever-offer indicator variable: it is equal to 1 for applicants that received an offer to get treated, and to 0 for all other applicants
	qui gen W = 10*groupid + All_present //W is the identifier of the waitlist the applicant participated in
	qui bysort W: egen S = total(D) //is the number of seats available in the waitlist the applicant participated in. S is equal to the number of applicants who received an offer and got treated in each waitlist
	qui randomizedwaitlist Y D Z W S if impact_survey_sample
	local column: word `column_no' of `c(ALPHA)'
	qui putexcel (`column'6) = `e(DREO_LATE)'
	qui putexcel (`column'7) = `e(se_DREO_LATE)'
	qui putexcel (`column'8) = (2 * ttail(e(nb_obs), abs(e(DREO_LATE)/e(se_DREO_LATE))))
	scalar DREO_LATE = e(DREO_LATE)
	qui sum `depvar' if impact_survey_sample & All_survey_status_1
	qui putexcel (`column'9) = (`r(mean)')
	qui putexcel (`column'10) = (scalar(DREO_LATE)/`r(mean)')
	qui sum `depvar' if impact_survey_sample & (All_survey_status_2|All_survey_status_3|All_survey_status_4|All_survey_status_5|All_survey_status_6|All_survey_status_7)
	qui putexcel (`column'11) = (`r(mean)')
	qui putexcel (`column'12) = (scalar(DREO_LATE)/`r(mean)')
	qui sum `depvar' if impact_survey_sample & !(All_survey_status_1|All_survey_status_2|All_survey_status_3|All_survey_status_4|All_survey_status_5|All_survey_status_6|All_survey_status_7)
	qui putexcel (`column'13) = (`r(mean)')
	qui putexcel (`column'14) = (scalar(DREO_LATE)/`r(mean)')
	*****Count number of subgroups
	local N_group        = 0
	local N_group_eff    = 0
	local N_subgroup     = 0
	local N_subgroup_eff = 0
	local N_indiv        = 0
	local N_indiv_eff    = 0
	tempvar survey_status
	gen `survey_status'  = Z
	tab `survey_status' if impact_survey_sample
	foreach subgroup of varlist groupid_* {
		local group_eff = 0
		qui sum `survey_status' if impact_survey_sample &  All_present & `subgroup'
		if `r(N)'>0 {
			local group_eff      = `group_eff' + (`r(max)'>`r(min)')
			local N_subgroup     = `N_subgroup' + 1
			local N_subgroup_eff = `N_subgroup_eff' + (`r(max)'>`r(min)')
			local N_indiv        = `N_indiv' + `r(N)'
			local N_indiv_eff    = `N_indiv_eff' + `r(N)'*(`r(max)'>`r(min)')
			}
		qui sum `survey_status' if impact_survey_sample & !All_present & `subgroup'
		if `r(N)'>0 {
			local group_eff      = `group_eff' + (`r(max)'>`r(min)')
			local N_subgroup     = `N_subgroup' + 1
			local N_subgroup_eff = `N_subgroup_eff' + (`r(max)'>`r(min)')
			local N_indiv        = `N_indiv' + `r(N)'
			local N_indiv_eff    = `N_indiv_eff' + `r(N)'*(`r(max)'>`r(min)')
			}
		qui sum `survey_status' if impact_survey_sample & `subgroup'
		if `r(N)'>0 {
			local N_group = `N_group' + 1
			}
		if `group_eff'>0 {
			local N_group_eff    = `N_group_eff' + 1
			}
		}
	qui putexcel (`column'15) = `N_group'
	qui putexcel (`column'16) = `N_group_eff'
	qui putexcel (`column'17) = `N_subgroup'
	qui putexcel (`column'18) = `N_subgroup_eff'
	qui putexcel (`column'19) = `N_indiv'
	qui putexcel (`column'20) = `N_indiv_eff'
	local column_no = `column_no' + 1
	qui drop Y D Z W S
	}
****Primary list vs. control list (Table 3, Columns 3-4)
*****Count number of subgroups
local N_group_eff    = 0
local N_subgroup     = 0
local N_subgroup_eff = 0
local N_indiv        = 0
local N_indiv_eff    = 0
tempvar survey_status
gen `survey_status'  = 0
forvalues i=1/7 {
	qui replace `survey_status' = `i'*All_survey_status_`i' if All_survey_status_`i'
	}
tab `survey_status' if impact_survey_sample
foreach subgroup of varlist groupid_* {
	local group_eff = 0
	qui sum `survey_status' if impact_survey_sample & !All_survey_status_replacement &  All_present & `subgroup'
	if `r(N)'>0 {
		local group_eff      = `group_eff' + (`r(max)'>`r(min)')
		local N_subgroup     = `N_subgroup' + 1
		local N_subgroup_eff = `N_subgroup_eff' + (`r(max)'>`r(min)')
		local N_indiv        = `N_indiv' + `r(N)'
		local N_indiv_eff    = `N_indiv_eff' + `r(N)'*(`r(max)'>`r(min)')
		}
	qui sum `survey_status' if impact_survey_sample & !All_survey_status_replacement & !All_present & `subgroup'
	if `r(N)'>0 {
		local group_eff      = `group_eff' + (`r(max)'>`r(min)')
		local N_subgroup     = `N_subgroup' + 1
		local N_subgroup_eff = `N_subgroup_eff' + (`r(max)'>`r(min)')
		local N_indiv        = `N_indiv' + `r(N)'
		local N_indiv_eff    = `N_indiv_eff' + `r(N)'*(`r(max)'>`r(min)')
		}
	if `group_eff'>0 {
		local N_group_eff    = `N_group_eff' + 1
		}
	}
*****Regressions
foreach depvar of varlist $depvars_adoption { //initial offer (IO) estimator: primary vs. control
	eststo: qui ivregress 2sls `depvar' $controls_IV (baseline_sample = All_survey_status_1) if impact_survey_sample & !All_survey_status_replacement, vce(cluster groupid)
		estadd local N_group_eff `N_group_eff'
		estadd local N_subgroup `N_subgroup'
		estadd local N_subgroup_eff `N_subgroup_eff'
		estadd local N_indiv_eff `N_indiv_eff'
		estadd local Controls_I "No"
		estadd local FE "Yes"
		qui sum `depvar' if impact_survey_sample & All_survey_status_1
		estadd scalar MeanPrimary               = r(mean)
		estadd scalar RelativeImpactPrimary     = _b[baseline_sample]/r(mean)
		qui sum `depvar' if impact_survey_sample & (All_survey_status_2|All_survey_status_3|All_survey_status_4|All_survey_status_5|All_survey_status_6|All_survey_status_7)
		estadd scalar MeanReplacement           = r(mean)
		estadd scalar RelativeImpactReplacement = _b[baseline_sample]/r(mean)
		qui sum `depvar' if impact_survey_sample & !(All_survey_status_1|All_survey_status_2|All_survey_status_3|All_survey_status_4|All_survey_status_5|All_survey_status_6|All_survey_status_7)
		estadd scalar MeanControl               = r(mean)
		estadd scalar RelativeImpactControl     = _b[baseline_sample]/r(mean)
	}
esttab using ".\06 tables\table_robustness_b.csv", keep(baseline_sample) b(3) star(* 0.10 ** 0.05 *** 0.01) se sfmt(%8.0g %05.3f) scalars( "N_clust Groups" "Controls_I Controlled for gender" "FE Subgroup fixed effects" "MeanPrimary Mean of primary list" "RelativeImpactPrimary Relative impact primary list" "MeanReplacement Mean of replacement list" "RelativeImpactReplacement Relative impact replacement list" "MeanControl Mean of control list" "RelativeImpactControl Relative impact control list" "N_group_eff Groups effective" "N_subgroup Subgroups" "N_subgroup_eff Subgroups effective" "N_indiv_eff Observations effective") title("Robustness impact of being surveyed") label replace
eststo clear
***Mechanisms
****Other imputs
*****Fertilizer use
gen bl_fertilizer = (BL_I_e1fertilizer&BL_I_e5_maize_1) if !missing(BL_I_e1fertilizer) //generate baseline variable
sum bl_fertilizer EL_c1inorganic_fertilizer if !missing(EL_c1inorganic_fertilizer) & !missing(bl_fertilizer) & EL_c1inorganic_fertilizer!=99 & impact_survey_sample //show levels
gen diff_fertilizer = EL_c1inorganic_fertilizer - bl_fertilizer if EL_c1inorganic_fertilizer!=99 & impact_survey_sample //generate difference
regress diff_fertilizer, vce(robust) //test significance of difference
*****Correlation between fertilizer use and aflasafe adoption
corr adoption EL_c1inorganic_fertilizer if EL_c1inorganic_fertilizer!=99 & impact_survey_sample
drop bl_fertilizer diff_fertilizer
*****Interaction w/ primary (Table 4)
foreach depvar of varlist $depvars_adoption {
	eststo: qui ivregress 2sls `depvar' $controls_IV (baseline_sample int_baseline_sample_primary = $instruments) if impact_survey_sample, vce(cluster groupid)
		estadd local Controls_I "No"
		estadd local FE "Yes"
		qui sum `depvar' if impact_survey_sample & All_survey_status_1
		estadd scalar MeanPrimary               = r(mean)
		estadd scalar RelativeImpactPrimary     = _b[baseline_sample]/r(mean)
		qui sum `depvar' if impact_survey_sample & (All_survey_status_2|All_survey_status_3|All_survey_status_4|All_survey_status_5|All_survey_status_6|All_survey_status_7)
		estadd scalar MeanReplacement           = r(mean)
		estadd scalar RelativeImpactReplacement = _b[baseline_sample]/r(mean)
		qui sum `depvar' if impact_survey_sample & !(All_survey_status_1|All_survey_status_2|All_survey_status_3|All_survey_status_4|All_survey_status_5|All_survey_status_6|All_survey_status_7)
		estadd scalar MeanControl               = r(mean)
		estadd scalar RelativeImpactControl     = _b[baseline_sample]/r(mean)
	}
foreach depvar of varlist $depvars_adoption {
	eststo: qui ivregress 2sls `depvar' $controls_IV All_sex (baseline_sample int_baseline_sample_primary = $instruments) if impact_survey_sample, vce(cluster groupid)
		estadd local Controls_I "Yes"
		estadd local FE "Yes"
		qui sum `depvar' if impact_survey_sample & All_survey_status_1
		estadd scalar MeanPrimary               = r(mean)
		estadd scalar RelativeImpactPrimary     = _b[baseline_sample]/r(mean)
		qui sum `depvar' if impact_survey_sample & (All_survey_status_2|All_survey_status_3|All_survey_status_4|All_survey_status_5|All_survey_status_6|All_survey_status_7)
		estadd scalar MeanReplacement           = r(mean)
		estadd scalar RelativeImpactReplacement = _b[baseline_sample]/r(mean)
		qui sum `depvar' if impact_survey_sample & !(All_survey_status_1|All_survey_status_2|All_survey_status_3|All_survey_status_4|All_survey_status_5|All_survey_status_6|All_survey_status_7)
		estadd scalar MeanControl               = r(mean)
		estadd scalar RelativeImpactControl     = _b[baseline_sample]/r(mean)
	}
esttab using ".\06 tables\table_mechanisms.csv", keep(baseline_sample int_baseline_sample_primary) b(3) star(* 0.10 ** 0.05 *** 0.01) se sfmt(%8.0g %05.3f) scalars("N_clust Groups" "Controls_I Controlled for gender" "FE Subgroup fixed effects" "MeanPrimary Mean of primary list" "RelativeImpactPrimary Relative impact primary list" "MeanReplacement Mean of replacement list" "RelativeImpactReplacement Relative impact replacement list" "MeanControl Mean of control list" "RelativeImpactControl Relative impact control list") title("Mechanisms impact of being surveyed") label replace
eststo clear

**Balance across primary and replacement respondents (Table 5)
preserve
***Create table
tempname memhold
tempfile balance
postfile `memhold' str80 Variable N1 Mean1 SD1 N0 Mean0 SD0 Pvalue PvalueControls PvalueControls2 using "`balance'", replace
***Calculate statistics for non-surveyed sample
foreach var of varlist $balance {
	scalar Variable = `"`: var label `var''"'
	qui su `var' if impact_survey_sample & All_survey_status_1==1
	scalar N1 = `r(N)'
	scalar Mean1 = `r(mean)'
	scalar SD1 = `r(sd)'
	qui su `var' if impact_survey_sample & All_survey_status_1==0
	scalar N0 = `r(N)'
	scalar Mean0 =`r(mean)'
	scalar SD0 = `r(sd)'
	qui reg `var' All_survey_status_1              if impact_survey_sample, vce(robust)
	scalar Pvalue         = ((2 * ttail(e(df_r), abs(_b[All_survey_status_1]/_se[All_survey_status_1]))))
	qui reg `var' All_survey_status_1 $controls_IV if impact_survey_sample, vce(robust)
	scalar PvalueControls = ((2 * ttail(e(df_r), abs(_b[All_survey_status_1]/_se[All_survey_status_1]))))
	qui reg `var' All_survey_status_1 $controls_IV All_sex if impact_survey_sample, vce(robust)
	scalar PvalueControls2 = ((2 * ttail(e(df_r), abs(_b[All_survey_status_1]/_se[All_survey_status_1]))))
	post `memhold' (Variable) (N1) (Mean1) (SD1) (N0) (Mean0) (SD0) (Pvalue) (PvalueControls) (PvalueControls2)
	scalar drop _all
	}
postclose `memhold'
***Export table
use "`balance'", clear
export excel ".\06 tables\table_balance_primary_replacement.xlsx", firstrow(variables) replace
***Restore dataset
restore

**Impact of market linkage for impact survey sample (Table 6)
gen temp_count_group = 0
bysort impact_survey_sample groupid: replace temp_count_group = _n==1 if impact_survey_sample
****Full sample
foreach depvar of varlist $depvars_adoption {
	eststo: qui regress `depvar' Treat_v_market_linkage                                          if impact_survey_sample                 		                        , vce(cluster Treat_village)
		estadd local Controls "No"
		estadd local Sample "Full"
		qui sum `depvar' 																	     if impact_survey_sample & Treat_v_market_linkage==0
		estadd scalar MeanControl = r(mean)
		qui count if temp_count_group
		estadd scalar N_group    = r(N)
	eststo: qui regress `depvar' Treat_v_market_linkage Treat_v_bundled_insurance $controls_ws_b if impact_survey_sample              			                        , vce(cluster Treat_village)
		estadd local Controls "Yes"
		estadd local Sample "Full"
		qui sum `depvar' 																	     if impact_survey_sample & Treat_v_market_linkage==0
		estadd scalar MeanControl = r(mean)
		qui count if temp_count_group
		estadd scalar N_group    = r(N)
	}
foreach depvar of varlist $depvars_adoption {
	eststo: qui regress `depvar' Treat_v_market_linkage                                          if impact_survey_sample & baseline_sample                  		    , vce(cluster Treat_village)
		estadd local Controls "No"
		estadd local Sample "Surveyed"
		qui sum `depvar' 																	     if impact_survey_sample & baseline_sample  & Treat_v_market_linkage==0
		estadd scalar MeanControl = r(mean)
		qui count if temp_count_group
		estadd scalar N_group    = r(N)
	eststo: qui regress `depvar' Treat_v_market_linkage Treat_v_bundled_insurance $controls_ws_b if impact_survey_sample & baseline_sample               			    , vce(cluster Treat_village)
		estadd local Controls "Yes"
		estadd local Sample "Surveyed"
		qui sum `depvar' 																	     if impact_survey_sample & baseline_sample  & Treat_v_market_linkage==0
		estadd scalar MeanControl = r(mean)
		qui count if temp_count_group
		estadd scalar N_group    = r(N)
	}
foreach depvar of varlist $depvars_adoption {
	eststo: qui regress `depvar' Treat_v_market_linkage                                          if impact_survey_sample & !baseline_sample                  		    , vce(cluster Treat_village)
		estadd local Controls "No"
		estadd local Sample "Non-surveyed"
		qui sum `depvar' 																	     if impact_survey_sample & !baseline_sample  & Treat_v_market_linkage==0
		estadd scalar MeanControl = r(mean)
		qui count if temp_count_group
		estadd scalar N_group    = r(N)
	eststo: qui regress `depvar' Treat_v_market_linkage Treat_v_bundled_insurance $controls_ws_b if impact_survey_sample & !baseline_sample               			    , vce(cluster Treat_village)
		estadd local Controls "Yes"
		estadd local Sample "Non-surveyed"
		qui sum `depvar' 																	     if impact_survey_sample & !baseline_sample  & Treat_v_market_linkage==0
		estadd scalar MeanControl = r(mean)
		qui count if temp_count_group
		estadd scalar N_group    = r(N)
	}
esttab using ".\06 tables\table_impact_market_linkage_for_impact_survey_sample.csv", keep(Treat_v_market_linkage) b(3) star(* 0.10 ** 0.05 *** 0.01) se sfmt(%8.0g %8.0g %8.0g %05.3f %8.0g) scalars("Controls Baseline controls" "Sample Sample" "N_clust Villages" "MeanControl Mean of no market linkage" "N_group Groups") title("Impact market linkage for impact survey sample") label replace
eststo clear
**Interaction w/ experimental treatment (Table 7)
foreach depvar of varlist $depvars_adoption {
	eststo: qui ivregress 2sls `depvar' $controls_IV (int_treat_baseline_sample baseline_sample = $int_treat_instruments $instruments) if impact_survey_sample, vce(cluster Treat_village)
		estadd local Controls_I "No"
		estadd local FE "Yes"
		qui count if temp_count_group
		estadd scalar N_group    = r(N)

	}
foreach depvar of varlist $depvars_adoption {
	eststo: qui ivregress 2sls `depvar' $controls_IV All_sex (int_treat_baseline_sample baseline_sample = $int_treat_instruments $instruments) if impact_survey_sample, vce(cluster Treat_village)
		estadd local Controls_I "Yes"
		estadd local FE "Yes"
		qui count if temp_count_group
		estadd scalar N_group    = r(N)
	}
esttab using ".\06 tables\table_interaction.csv", keep(int_treat_baseline_sample baseline_sample) b(3) star(* 0.10 ** 0.05 *** 0.01) se sfmt(%8.0g) scalars("N_clust Villages" "Controls_I Controlled for gender" "FE Subgroup fixed effects" "N_group Groups") title("Interaction with treatment") label replace
eststo clear
drop temp_count_group

*FINALIZE
log close
translate ".\04 logs\analysis_impact_surveys.log" ".\04 logs\analysis_impact_surveys.pdf"
erase ".\04 logs\analysis_impact_surveys.log"
